SciPy

SciPy is another add on library for scientific computing. I will only go over some small examples, but I urge you to check out the documentation on your own as it will be selectively useful to you in the future. The SciPy documentation can be found at: http://docs.scipy.org/doc/scipy/reference/

There are a couple of important libraries that you should know about.

scipy.integrate -> Integration and ODE Solvers
scipy.interpolate -> Interpolation and Smoothing Splines
scipy.linalg -> Linear Algebra
scipy.optimize -> Optimization
scipy.signal -> Signal Processing
scipy.stats -> Statistics

To import these libraries, we use the following syntax

import scipy.package as <nickname>

For example

import scipy.stats as stat

Then, when you call a function in that library, you use

stat.norm.pdf()

In [34]:
import scipy.optimize as opt
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as inter
%matplotlib inline

In [31]:
#Let's find out what the scipy.optimize.curve_fit function does and how to use it.
opt.curve_fit?

In [28]:
x = np.linspace(0,10, 200)
y = 34*np.sin(x)*np.random.rand(200)

plt.plot(x,y)


Out[28]:
[<matplotlib.lines.Line2D at 0x7f629fb79048>]

In [33]:
func = lambda a,x: a*np.sin(x)
popt, pcov = opt.curve_fit(func, x, y, p0=[10])
print(popt)
plt.plot(np.linspace(0,10,1000), func(popt[0], np.linspace(0,10,1000)))
plt.plot(x,y)


[ 8.87147039]
Out[33]:
[<matplotlib.lines.Line2D at 0x7f629fb014e0>]

As we can see, scipy helps us fit data to theoretical curves and even gives us variance estimates on our parameters. Let's check out one more SciPy example before moving on.


In [35]:
inter.interp1d?

In [43]:
x = np.logspace(0,1.1,20)
y = np.exp(np.sin(x))
plt.plot(x,y, 'ro')


Out[43]:
[<matplotlib.lines.Line2D at 0x7f629e6ee748>]

In [73]:
f = inter.interp1d(x,y, kind=3)

plt.plot(np.linspace(1,12.5,1000),f(np.linspace(1,12.5,1000)))
plt.plot(x,y,'ro')


Out[73]:
[<matplotlib.lines.Line2D at 0x7f629de28748>]

There are lots of other SciPy tols that you can use for all kinds of scientific computing. Go check out the SciPy documentation for more information.


In [ ]: